package com.pigotech.ptwo.UI.Activity;

import android.app.TabActivity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TabHost;
import android.widget.Toast;

import com.pigotech.ptwo.R;
import com.pigotech.ptwo.UI.CustomView.CustomToast;

/**
 *
 */
public class MainActivity extends TabActivity implements View.OnClickListener {

    private static final String VIDEO_TAG = "video";                                    //底栏视频主页键  的tag
    private static final String SETTING_TAG = "setting";                                //底栏文档按钮    的tag
    private static final String DOC_TAG = "doc";                                        //底栏设置按钮    的tag
    private static final String HELPER_TAG = "helper";                                  //底栏帮助文档按钮的tag
    private static final String FEEDBACK_TAG = "feedback";                              //意见反馈按钮tag

    private TabHost tabHost;
    //    private RadioGroup rdoGroup;
    private LinearLayout ll_bottom_radiogroup;                                          //底部tabhost的寄存布局
    RadioButton rdoBtnHome;                                                            //底栏视频主页键
    RadioButton rdoBtnDoc;                                                             //底栏文档按钮
    RadioButton rdoBtnSetting;                                                         //底栏设置按钮
    RadioButton rdoBtnHelper;                                                          //底栏帮助文档按钮
    RadioButton rdoBtnFeedback;

    private View viewBottom;                                                           //底部tabhost所在的view（可控制其显示隐藏）

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { // 4.4系统以上开启透明状态栏
            //透明状态栏
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            //透明底部导航栏
//            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
        }
        initView();
    }

    private void initView() {
        rdoBtnHome = (RadioButton) findViewById(R.id.main_btn_video);
        rdoBtnDoc = (RadioButton) findViewById(R.id.main_btn_doc);
        rdoBtnSetting = (RadioButton) findViewById(R.id.main_btn_setting);
        tabHost = getTabHost();
        addTab(VIDEO_TAG, MonitorActivity.class);
        addTab(DOC_TAG, DocumentActivity.class);
        addTab(HELPER_TAG, HelpCenterActivity.class);
        viewBottom = findViewById(R.id.ll_bottom_radiogroup);
        rdoBtnHome.setChecked(true);
        rotateScreen();
        rdoBtnHome.setOnClickListener(this);
        rdoBtnDoc.setOnClickListener(this);
        rdoBtnSetting.setOnClickListener(this);
    }

    /**
     * 给底部按钮添加对应的activity
     *
     * @param tag
     * @param c
     */
    private void addTab(String tag, Class<?> c) {
        Intent intent = new Intent(this, c);
        TabHost.TabSpec tabSpec = tabHost.newTabSpec(tag);
        tabSpec.setIndicator("");
        tabSpec.setContent(intent);
        tabHost.addTab(tabSpec);
    }

    /**
     * 判断横竖屏幕 隐藏tab选项卡
     */
    private void rotateScreen() {
        int mCurrentOrientation = getResources().getConfiguration().orientation;
        if (mCurrentOrientation == Configuration.ORIENTATION_PORTRAIT) {
//            rdoGroup.setVisibility(View.VISIBLE);
            viewBottom.setVisibility(View.VISIBLE);
        } else if (mCurrentOrientation == Configuration.ORIENTATION_LANDSCAPE) {
//            rdoGroup.setVisibility(View.GONE);
            viewBottom.setVisibility(View.GONE);
        }
    }

    /**
     * 底部状态栏触摸事件监听
     */
    private View.OnTouchListener onTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            changed(v.getId());
            setRadioBtnColor();
            return true;
        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.main_btn_video:
                changed(v.getId());
                setRadioBtnColor();
                break;
            case R.id.main_btn_doc:
                changed(v.getId());
                setRadioBtnColor();
                break;
            case R.id.main_btn_setting:
                changed(v.getId());
                setRadioBtnColor();
                break;
        }
    }

    /**
     * 切换页面
     * @param id activity对应的radioButton的id
     */
    private void changed(int id) {
        switch (id) {
            case R.id.main_btn_video:
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
                tabHost.setCurrentTabByTag(VIDEO_TAG);
                break;
            case R.id.main_btn_doc:
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                tabHost.setCurrentTabByTag(DOC_TAG);
                break;
            case R.id.main_btn_setting:
                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                tabHost.setCurrentTabByTag(HELPER_TAG);
                break;
        }
    }

    /**
     * 设置底部状态栏颜色
     */
    private void setRadioBtnColor() {
        if (rdoBtnHome.isChecked()) {
            rdoBtnHome.setBackgroundColor(getResources().getColor(R.color.botton_cheecked));
        } else {
            rdoBtnHome.setBackgroundColor(getResources().getColor(R.color.botton_uncheecked));

        }
        if (rdoBtnSetting.isChecked()) {
            rdoBtnSetting.setBackgroundColor(getResources().getColor(R.color.botton_cheecked));
        } else {
            rdoBtnSetting.setBackgroundColor(getResources().getColor(R.color.botton_uncheecked));

        }
        if (rdoBtnDoc.isChecked()) {
            rdoBtnDoc.setBackgroundColor(getResources().getColor(R.color.botton_cheecked));
        } else {
            rdoBtnDoc.setBackgroundColor(getResources().getColor(R.color.botton_uncheecked));
        }
    }

}
